package com.yjj.demo.aop.component;

import com.alibaba.fastjson.JSONObject;
import com.yjj.demo.aop.annotation.LogAspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 *
 *
 * @author yangjiajia
 * @time 2021/8/22 0:11
 */
@Aspect
@Component
public class LogAspectComponent {

    private Logger logger = LoggerFactory.getLogger(LogAspectComponent.class);

    /**
     * TODO userId获取方法
     * @param logAspect
     * @author yangjiajia
     * @date 2021/8/22 10:52
     * @return void
     **/
    @Before("@annotation(logAspect)")
    public void before(LogAspect logAspect) {
        logger.info("before module:{},type:{}", logAspect.module().getDesc(), logAspect.type().name());
    }

    @Around("@annotation(logAspect)")
    public Object process(ProceedingJoinPoint point, LogAspect logAspect) {
        try {
            Object[] args = point.getArgs();
            if (args.length > 0) {
                logger.info("around args:{}", JSONObject.toJSONString(args));
            }
            Object returnValue = point.proceed(args);
            logger.info("around module:{},type:{},returnValue:{}", logAspect.module().getDesc(), logAspect.type().name(), returnValue);
            return returnValue;
        } catch (Throwable e) {
            logger.error("log aspect 异常");
            logger.error(e.getMessage(), e);
        }
        return null;
    }
}
